<?php

/**
 * matches actions.
 *
 * @package    bgMicroLeague
 * @subpackage matches
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class matchesActions extends sfActions
{

  public function executeListBoardgamePlayer(sfWebRequest $request)
  {
    $this->clearFilters();

    $this->setLeagueOr404();
    $this->setBoardgameOr404($request);
    $this->setPlayerOr404($request);
    
    $this->gameList = GameQuery::create()->
            getByLeagueAndBoardgameAndPlayerListCriteria($this->league, $this->boardgame, $this->player)->
            paginate($request->getParameter("page"), sfConfig::get('max_match_per_page', 10));

    $this->populateMatchRelation();
  }

  public function executeListPlayer(sfWebRequest $request)
  {
    $this->clearFilters();

    $this->setLeagueOr404();
    $this->setPlayerOr404($request);

    $this->gameList = GameQuery::create()->
            getByLeagueAndPlayerListCriteria($this->league, $this->player)->
            paginate($request->getParameter("page"), sfConfig::get('max_match_per_page', 10));

    $this->populateMatchRelation();
  }

  public function executeListBoardgame(sfWebRequest $request)
  {
    $this->clearFilters();

    $this->setLeagueOr404();
    $this->setBoardgameOr404($request);

    $this->gameList = GameQuery::create()->
            getByLeagueAndBoardgameListCriteria($this->league, $this->boardgame)->
            paginate($request->getParameter("page"), sfConfig::get('max_match_per_page', 10));

    $this->populateMatchRelation();
  }

  public function executeListAll(sfWebRequest $request)
  {
    $this->filterForm = new GameFormFilter($this->getFilters());
    
    $this->gameList = $this->getPager($request);

    $this->populateMatchRelation();
  }

  private function populateMatchRelation()
  {
    //Si è resa necessaria in BasePeer linea 860 la modifica da
    //$joinClause[] = $join->getJoinType() . ' ' . $rightTableForFrom . " ON ($condition)";
    //a
    //$joinClause[] = $join->getJoinType() . ' `' . $rightTableForFrom . "` ON ($condition)";
    $this->gameList->getResults()->populateRelation('PlayerGame',  PlayerGameQuery::create()->orderByPlacing(Criteria::ASC)->join('Player')->with('Player'));
  }

  private function setLeagueOr404()
  {
    $idLeague = $this->getIdLeagueAttribute();

    $this->league = LeagueQuery::create()->
                      findOneById($idLeague);

    $this->forward404If(!$this->league);
  }

  protected function getIdLeagueAttribute()
  {
    return $this->getUser()->getAttribute('selectedIdLeague',  null, sfConfig::get('application_namespace'));
  }

  private function setBoardgameOr404(sfWebRequest $request)
  {
    $idBoardgame = $request->getParameter('idBoardgame');

    $this->boardgame = BoardgameQuery::create()->
                    findOneById($idBoardgame);

    $this->forward404If(!$this->boardgame);
  }

  private function setPlayerOr404(sfWebRequest $request)
  {
    $username = $request->getParameter('username', null);

    $this->player = PlayerQuery::create()->
                    findOneByUsername($username);

    $this->forward404If(!$this->player);
  }

  protected function getFilters()
  {
    return $this->getUser()->getAttribute($this->getModuleName() . '.filters',  array(), sfConfig::get('application_namespace'));
  }


  protected function clearFilters()
  {
    $this->getUser()->setAttribute($this->getModuleName() . '.filters', null, sfConfig::get('application_namespace'));
  }
  
  protected function getPager(sfWebRequest $request)
  {
    $query = $this->buildQuery();
    $pager = $query->paginate($request->getParameter("page"), sfConfig::get('max_match_per_page', 10));

    return $pager;
  }
  
  protected function buildQuery()
  {
    $query = $this->filterForm->buildCriteria($this->getFilters());
    
    $query = $query->getListCriteria();
    
    return $query;
  }
}
